﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using BCGC.BLL.BusinessObject.Resource;
using Jalal.Library.DataStructure;
using Jalal.Library.Utility;
using System.Collections.Generic;

public partial class Resources_Wallpaper : PublicBasePage
{
    private const int pageSize = 9;
    private int Id
    {
        get
        {
            if (ViewState["Id"] == null)
                ViewState["Id"] = 0;
            return (int)ViewState["Id"];
        }
        set { ViewState["Id"] = value; }
    }

    private int CurrentPageIndex
    {
        get
        {
            if (ViewState["CurrentPageIndex"] == null)
                ViewState["CurrentPageIndex"] = 1;
            return (int)ViewState["CurrentPageIndex"];
        }
        set { ViewState["CurrentPageIndex"] = value; }
    }

    private string SortOrder
    {
        get
        {
            if (ViewState["SortOrder"] == null)
                ViewState["SortOrder"] = "WallpaperId";
            return (string)ViewState["SortOrder"];
        }
        set { ViewState["SortOrder"] = value; }
    }

    private int TotalData
    {
        get
        {
            if (ViewState["TotalData"] == null)
                ViewState["TotalData"] = 0;
            return (int)ViewState["TotalData"];
        }
        set { ViewState["TotalData"] = value; }
    }

    private int PageSize
    {
        get
        {
            if (ViewState["PageSize"] == null)
                ViewState["PageSize"] = 9;
            return (int)ViewState["PageSize"];
        }
        set { ViewState["PageSize"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int id = Id = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["id"]) && int.TryParse(Request.QueryString["id"], out id))
            {
                Id = id;
                LoadData();
                BindPager();
            }
            else
                Response.Redirect(CustomSiteMap.Resources);
        }
    }

    private void LoadData()
    {
        int totalData = 0;
        ReadWriteCustomCollection<Wallpaper> wallpapers = Wallpaper.GetAllWallpapersByGame(CurrentPageIndex, SortOrder, PageSize,
            Id, out totalData);

        TotalData = totalData;
       
        dtlWallpapers.DataSource = wallpapers;
        dtlWallpapers.DataBind();
    }

    protected void DataList_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        Wallpaper item = e.Item.DataItem as Wallpaper;
        Repeater sizeList = e.Item.FindControl("rptSizeList") as Repeater;
        if (sizeList != null)
        {
            sizeList.DataSource = item.Sizes;
            sizeList.DataBind();
        }
    }

    protected void Size_DataBound(object sender, RepeaterItemEventArgs e)
    {
        WallpaperSize wallpaperSize = e.Item.DataItem as WallpaperSize;
        HyperLink hlkSizeLink = e.Item.FindControl("hlkSizeLink") as HyperLink;
        if (hlkSizeLink!= null && wallpaperSize != null)
        {
            hlkSizeLink.Attributes.Add("onclick", "javascript:getWall('" + wallpaperSize.SizeName + "'," + wallpaperSize.WallpaperSizeId.ToString() + ",'');return false;");
        }
    }

    private void BindPager()
    {
        List<string> pages = new List<string>();
        int pageCount = (TotalData / PageSize) + ((TotalData % PageSize) > 0 ? 1 : 0);
        for (int i = 1; i <= pageCount; i++)
            pages.Add(i.ToString());

        rptPager.DataSource = pages;
        rptPager.DataBind();
    }

    protected void Page_Change(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "PagingCommand")
        {
            CurrentPageIndex = Convert.ToInt32(e.CommandArgument);
            LoadData();
            BindPager();
        }
    }

    protected void Data_Sort(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "Sort")
        {
            SortOrder = (string)e.CommandArgument;
            LoadData();
        }
    }

    protected void Search_Click(object sender, EventArgs e)
    {
        LoadData();
        BindPager();
    }

    protected void Pager_DataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            if (Convert.ToInt32(e.Item.DataItem) == CurrentPageIndex)
            {
                LinkButton lkbPageIndex = (LinkButton)e.Item.FindControl("lkbPageIndex");
                lkbPageIndex.CssClass = "page-index-selected";
            }
        }
    }
}
